//
//  YanNetworkManager+Extension.swift
//  SinaWeibo
//
//  Created by mac on 2017/5/3.
//  Copyright © 2017年 mac. All rights reserved.
//

import Foundation

// MARK: - 封装新浪微博的网络请求方法
extension YanNetworkManager {
    func statusList(since_id: Int64, max_id: Int64, completion:@escaping (_ list: [[String: Any]], _ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        // Swift中Int可以转换成AnyObject,但Int64不行
        let params = ["since_id": "\(since_id)", "max_id": "\(max_id > 0 ? max_id - 1 : 0)"]
        
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            let result = json as? [String: Any]
            let list = result?["statuses"] as? [[String: Any]]
            completion(list ?? [], isSuccess)
        }
        
        
        
    }
    
    func unreadCount(completion: @escaping (_ count: Int)->()) {
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            let dict = json as? [String: Any]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
        }
   
    }
    
}

// MARK: - 用户信息
extension YanNetworkManager {
    
    /// 加载用户信息 - 用户登录后立即执行
    func loadUserInfo(completion: @escaping (_ dict: [String: Any])->()) {
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid": uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            // 完成回调
            completion((json as? [String: Any]) ?? [:])
        }
    }
}

// MARK: - OAuth相关方法
extension YanNetworkManager {
    
    /// 加载AccessToken
    ///
    /// - Parameters:
    ///   - code: 授权码
    ///   - completion: 完成回调
    func loadAccessToken(code: String, completion: @escaping (_ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id": WBAppKey,
                      "client_secret": WBAppSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": WBRedirectURI]
        
        // 发起网络请求
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            print(json as Any)
            // 直接用字典设置userAccount的属性
            self.userAccount.yy_modelSet(with: (json as? [String: Any]) ?? [:])
            
            
            // 加载当前用户信息
            self.loadUserInfo(completion: { (dict) in
                print(dict)
                // 使用用户字典设置用户账户信息
                self.userAccount.yy_modelSet(with: dict)
                // 保存模型
                self.userAccount.saveAccount()
                // 完成回调
                completion(isSuccess)
            })
            
            
        }
    }
}
